<template>
  <div class="publish-message">
      <div class="container">
          <div class="publish-message-title text-overflow">编辑推送平台信息</div>
          <!-- 编辑信息 -->
          <div class="form-box">
              <el-form ref="messageForm" :model="messageForm" label-width="100px">
                <el-form-item label="推送对象">
                  <el-radio-group v-model="messageForm.all_send">
                    <el-radio :label="1">品牌推送</el-radio>
                    <el-radio :label="2">全平台推送</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="品牌" v-if="messageForm.all_send === 1">
                  <el-select v-model="messageForm.brand_id" placeholder="请选择">
                    <el-option
                      v-for="item in brand_list"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="推送标题">
                  <el-input placeholder="请输入标题" v-model="messageForm.alert"></el-input>
                </el-form-item>
                <el-form-item label="持续时间">
                   <el-date-picker
                      v-model="messageForm.end_time"
                      type="datetime"
                      placeholder="选择日期时间"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="推送内容">
                  <el-input
                    type="textarea"
                    :rows="6"
                    placeholder="请输入内容"
                    v-model="messageForm.content">
                  </el-input>
                </el-form-item>             
                <el-form-item>
                   <el-button type="danger" size="medium" style="width:200px" @click="submitMessage">发送</el-button>
                </el-form-item>
              </el-form>
              <div class="hot-tips">
                <span>温馨提示：</span>
                <span class="tips-red" v-if="messageForm.all_send === 1">* 选择相应的贴牌后，填写对应的推送标题和推送主题内容，点击发送将推送消息至贴牌APP的所有用户</span>
                <span class="tips-red" v-else>* 此功能将给全体贴牌平台发送推送通知;(请谨慎使用)填写对应的推送标题和推送主题内容，点击发送将推送消息至贴牌APP的所有用户</span>
              </div>
          </div>



      </div>
  </div>
</template>

<script>
import USB from "@/utils/USB.js"; // 获取url
export default {
  name:'publish-message',
  data () {
    return {
        messageForm:{
            brand_id:null,
            alert:null,
            content:null,
            end_time:null,
            all_send:1
        },
        brand_list:[]
    };
  },
  components: {},
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init(){
      this.$Http.get(USB.U14).then(res=>{
        if(res.data.resp_code === "000000"){
          this.brand_list = res.data.result;
        }else{
          this.$notify.info({
            title: '提示',
            message: res.data.resp_message
          });
        }
      }).catch(err =>{
        this.$notify.error({
            title: '提示',
            message: "获取品牌信息失败！"
        });
      })
    },
    submitMessage(){
      // 提交信息
      if(this.messageForm.alert===""||this.messageForm.alert===null){
          this.$message.error("请输入标题！")
        return
      } 
      if(this.messageForm.content===""||this.messageForm.content===null){
        this.$message.error("请输入内容！")
        return
      } 
      if(this.messageForm.end_time===""||this.messageForm.end_time===null){
        this.$message.error("请选择时间！")
        return
      } 
      if(this.messageForm.all_send === 1){
         // 品牌推送
         if(this.messageForm.brand_id===""||this.messageForm.brand_id===null){
            this.$message.error("请选择品牌！")
            return
          } 
        let params = `brand_id=${this.messageForm.brand_id}&alert=${this.messageForm.alert}&content=${this.messageForm.content}&end_time=${this.messageForm.end_time}`,
            url = `user/jpush/tset/brand`;
            params = encodeURI(params); // 存在用户输入 需要编码下
        this.$Http.post(url,params).then(res=>{
           if(res.data.resp_code === "000000"){
              // 成功
           }
           this.$notify.info({
              title: '错误',
              message: res.data.resp_message
            })
        }).catch(err =>{
            this.$notify.info({
              title: '错误',
              message: "推送信息失败！"
            })
        })
      }else if(this.messageForm.all_send === 2){
        // 全平台推送
        let params = `alert=${this.messageForm.alert}&content=${this.messageForm.content}&end_time=${this.messageForm.end_time}`,
            url = `user/jpush/tset/brands`;
            params = encodeURI(params); // 存在用户输入 需要编码下
        this.$Http.post(url,params).then(res=>{
           if(res.data.resp_code === "000000"){
              // 成功
           }
           this.$notify.info({
              title: '错误',
              message: res.data.resp_message
            })
        }).catch(err =>{
            this.$notify.info({
              title: '错误',
              message: "推送信息失败！"
            })
        })
      }else{
        this.$notify.error({
          title: '错误',
          message: "找不到对应方式的推送"
        })
      }
    }
  }
}

</script>
<style scoped>
.publish-message{
  box-sizing: border-box;
  padding: 20px;
}
.container{
  border: 1px solid #ededed;
}
.publish-message-title{
  height: 50px;
  line-height: 50px;
  font-size: 26px;
  color: #409eff;
  padding: 5px 20px;
  border-bottom: 1px solid #ededed;
}
.form-box{
  padding: 20px 10px;
  max-width: 600px;
}
.hot-tips{
  padding-left: 30px;
}
.tips-red{
  color: red;
}
</style>